package org.jflame.commons.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.apache.commons.lang3.SystemUtils;

public class SysHelper extends SystemUtils {

    /**
     * 判断是否在docker环境运行
     * 
     * @return
     */
    public static boolean isDockerEnv() {
        String line;
        try (BufferedReader reader = Files.newBufferedReader(Paths.get("/proc/1/cgroup"))) {
            while ((line = reader.readLine()) != null) {
                if (line.contains("/docker/")) {
                    return true;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 判断是否在k8s环境下运行
     * 
     * @return
     */
    public static boolean isK8SEnv() {
        return StringHelper.isNotEmpty(System.getenv("KUBERNETES_SERVICE_HOST"));
    }
}
